<?php
namespace Admin\Controller;
use Tools\AdminController,    Model\UserModel,
    Think\Verify;

class LoginController extends AdminController {
    public function login(){
        $user = new UserModel();
        
        //退出或已登陆操作
        if(!empty($_GET)) {
            session('admin_id',NULL);
            session('admin_name',NULL);
            session('lasttime',NULL);
        }
  
        //登陆信息
        if(!empty($_POST)) {
            //验证验证码
            $vrf = new Verify();
            if($vrf->check($_POST['captcha'])) {
                //验证账号密码
                $info = $user -> checkNamePwd($_POST['user_name'],$_POST['user_pwd']);
                if($info) {
                    session('admin_id',$info['user_id']);
                    session('admin_name',$info['user_name']);
                    session('lasttime',$info['user_lasttime']);
                    //更新数据
                    $up['user_id']       = $info['user_id'];
                    $up['user_lasttime'] = time();
                    $up['user_lastip']   =  $_SERVER["REMOTE_ADDR"];
                    $user -> save($up);
                    $this ->redirect('Index/index');
                }else {
                    $this  -> alert("用户名或密码错误");
                }
                
            }else {
                $this  -> alert("验证码不正确");
            }
        }
        
        $this->display();
     }
     
     
     function verifyImg(){
         //输出验证码图片
         $config = array(
             'fontSize'  =>  20,        // 验证码字体大小(px)
             'imageH'    =>  38,        // 验证码图片高度
             'imageW'    =>  142,       // 验证码图片宽度
             'length'    =>  4,         // 验证码位数
             'fontttf'   =>  '4.ttf',   // 验证码字体，不设置随机获取
         );
         $very = new Verify($config);
         
         $very ->entry();       
     }
     
}